<template>
  <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="120px"
      class="ruleForm"
      size="large"
  >
    <el-form-item label="用户名" prop="">
      <el-input v-model="ruleForm.username" type="text" autocomplete="off" >
        <template #prefix>
          <el-icon><User /></el-icon>
        </template>
      </el-input>
    </el-form-item>
    <!--密码-->
    <el-form-item label="密码" prop="pass">
      <el-input v-model="ruleForm.password" type="password" autocomplete="off" >
        <template #prefix>
          <el-icon><Lock /></el-icon>
        </template>
      </el-input>
    </el-form-item>
    <!--重复密码-->
    <el-form-item label="重复密码" prop="checkPass">
      <el-input
          v-model="ruleForm.checkPass"
          type="password"
          autocomplete="off"
      >
        <template #prefix>
          <el-icon><Hide /></el-icon>
        </template>
      </el-input>
    </el-form-item>
    <!--手机号码-->
    <el-form-item label="手机号" prop="phone">
      <el-input v-model.number="ruleForm.phone" >
        <template #prefix>
          <el-icon><Iphone /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item>
      <!--返回-->
      <el-button @click="reLogin">
        <el-icon><ArrowLeftBold /></el-icon>
      </el-button>

      <!--提交-->
      <el-button type="primary" @click="reSubmitForm(ruleFormRef)"
      >提交</el-button>
      <!--重置-->
      <el-button @click="resetForm(ruleFormRef)">
        <el-icon size="18px"><Refresh /></el-icon>
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import {reactive, ref, defineEmits, inject} from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import * as userApi from "@/api/user.js"
import router from "../../router";

const reload = inject('reload')
const ruleFormRef = ref<FormInstance>()
const emits = defineEmits()

const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入手机号码'))
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error('请输入数字'))
    } else {
      if (value.toString().length !== 11) {
        callback(new Error('请输入正确的手机号码'))
      } else {
        callback()
      }
    }
  }, 500)
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== ruleForm.password) {
    callback(new Error("两次密码不匹配!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  username:'',
  password: '',
  checkPass: '',
  phone: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
  phone: [{ validator: checkPhone, trigger: 'blur' }],
})

const reSubmitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      userApi.register(ruleForm).then(resp=>{
        if (resp.code === 1){
          ElMessage({showClose: false, message: '注册成功，请重新登录', type: 'success'})
          reload()
        }else if (resp.code === 0){
          ElMessage({showClose: false, message: resp.msg, type: 'error'})
        }
      })
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const reLogin = () => {
  emits('relogin')
}
</script>
